package com.hnucm.house.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment<V,P extends BasePresenter<V>> extends Fragment {
    protected P mPresenter;

    protected Unbinder mUnbinder;

    protected abstract int getContentViewId();

    protected View mRootView;

//    protected LoadService mLoadService;

    //初始化
    protected abstract void init();

    //创建P
    protected abstract P createPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(mRootView == null){
            mRootView = inflater.inflate(getContentViewId(),container,false);
        }
        mPresenter = createPresenter();
        mUnbinder = ButterKnife.bind(this,mRootView);
        if(mPresenter != null){
            mPresenter.attchView((V) this);
        }
        init();
        return mRootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mUnbinder != null){
            mUnbinder.unbind();
        }
        if(mPresenter != null){
            mPresenter.detachView();
        }
    }
}
